<template>
  <div>
    <!-- 搜索 -->
    <el-row :gutter="20">
      <el-col :span="7">
        <el-input placeholder="请输入内容">
          <el-button slot="append" icon="el-icon-search"></el-button>
        </el-input>
      </el-col>
      <el-col :span="2">
        <el-button type="primary" @click="addDialogVisible = true"
          >添加用户</el-button
        >
      </el-col>
    </el-row>
    <!-- 用户列表 -->
    <!-- <el-table :data="userList"  stripe> -->
    <el-table :data="userList" stripe>
      <el-table-column type="index"></el-table-column>
      <el-table-column
        width="100"
        label="用户名"
        prop="username"
      ></el-table-column>
      <el-table-column
        width="100"
        label="密码"
        prop="password"
      ></el-table-column>
      <el-table-column width="100" label="性别" prop="sex"></el-table-column>
      <el-table-column label="时间" prop="loginDate"></el-table-column>
      <el-table-column
        width="200"
        label="手机号"
        prop="phoneNumber"
      ></el-table-column>
      <el-table-column width="100" label="会员" prop="vip"></el-table-column>
      <el-table-column label="邮箱" prop="email"></el-table-column>
      <el-table-column label="操作">
        <!-- <template slot-scope="scope"> -->
        <template slot-scope="scope">
          <!-- 修改 -->
          <el-tooltip
            effect="dark"
            content="修改"
            placement="top"
            :enterable="false"
          >
            <el-button
              @click="showEditDialog(scope.row)"
              type="primary"
              icon="el-icon-edit"
              size="mini"
            ></el-button>
          </el-tooltip>
          <!-- 删除 -->
          <el-tooltip
            effect="dark"
            content="删除"
            placement="top"
            :enterable="false"
          >
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeUserById(scope.row.uid)"
            ></el-button>
          </el-tooltip>
          <!-- 权限设置 -->
          <el-tooltip
            effect="dark"
            content="权限设置"
            placement="top"
            :enterable="false"
          >
            <el-button
              type="warning"
              icon="el-icon-setting"
              size="mini"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页区域 -->
    <!-- :total="userList.length" -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      layout="total, prev, pager, next"
      :page-size="page1"
    >
    </el-pagination>
    <!-- 添加用户的对话框 -->
    <el-dialog
      title="添加用户"
      :visible.sync="addDialogVisible"
      width="50%"
      @close="addDialogClosed"
    >
      <!-- 主体区 -->
      <el-form
        :model="addFrom"
        :rules="addFromRules"
        ref="addFromRef"
        label-width="70px"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="addFrom.username"></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input v-model="addFrom.password"></el-input>
        </el-form-item>

        <el-form-item label="手机号" prop="phoneNumber">
          <el-input v-model="addFrom.phoneNumber"></el-input>
        </el-form-item>

        <el-form-item label="邮箱" prop="email">
          <el-input v-model="addFrom.email"></el-input>
        </el-form-item>
      </el-form>
      <!-- 尾部 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUser">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改用户的对话框 -->
    <el-dialog title="修改用户" :visible.sync="editDialogVisible" width="50%">
      <el-form
        ref="editFromRef"
        :model="editFrom"
        :rules="editFromRules"
        label-width="70px"
      >
        <!-- 用户名 -->
        <el-form-item label="用户名">
          <el-input v-model="editFrom.username"></el-input>
        </el-form-item>
        <!-- 手机号 -->
        <el-form-item label="手机号">
          <el-input v-model="editFrom.phoneNumber"></el-input>
        </el-form-item>
        <!-- 邮箱 -->
        <el-form-item label="邮箱">
          <el-input v-model="editFrom.email"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEdit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
// import axios from "axios";
import {
  userList1,
  removeUser,
  addUsername,
  changeUsername,
} from "../api/Login";
export default {
  data() {
    // 验证邮箱的规则
    var checkEmail = (rule, value, cb) => {
      const regEmail = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
      if (regEmail.test(value)) {
        return cb();
      }
      cb(new Error("请输入合法的邮箱"));
    };
    // 验证手机号的规则
    var checkphone = (rule, value, cb) => {
      const regPhone = /^[1](([3][0-9])|([4][5-9])|([5][0-3,5-9])|([6][5,6])|([7][0-8])|([8][0-9])|([9][1,8,9]))[0-9]{8}$/;
      if (regPhone.test(value)) {
        return cb();
      }
      cb(new Error("请输入合法的手机号"));
    };

    return {
      // 用户列表
      userList: [],

      // 分页显示第一页
      page1: 1,

      // 控制添加对话框的显示与隐藏
      addDialogVisible: false,

      // 添加用户的表单数据
      addFrom: {
        username: "",
        password: "",
        phoneNumber: "",
        email: "",
      },

      // 添加表单验证规则的对象
      addFromRules: {
        // 用户名
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "用户名的长度在3~10之间",
            trigger: "blur",
          },
        ],
        // 密码
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "密码的长度在3~10之间",
            trigger: "blur",
          },
        ],
        // 手机号
        phoneNumber: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          { validator: checkphone, trigger: "blur" },
        ],
        // 邮箱
        email: [
          { required: true, message: "请输入邮箱", trigger: "blur" },
          { validator: checkEmail, trigger: "blur" },
        ],
      },
      editFromRules: {},
      // 修改用户对话框的显示与隐藏
      editDialogVisible: false,

      // 查询到的用户信息对象
      editFrom: {},
    };
  },
  // 调用
  created() {
    this.get_data();
  },
  methods: {
    // 获取用户列表
    get_data() {
      userList1({
        limit: 50,
        page: this.page1,
      }).then((res) => {
        this.userList = res.data.list;
      });
    },
    // 修改用户 的对话框
    showEditDialog(row) {
      console.log(row, "res");
      this.editFrom = { ...row };
      this.editDialogVisible = true;
    },
    // 提交修改
    submitEdit() {
      this.editDialogVisible = false;
      changeUsername(this.editFrom).then((res) => {
        console.log(res, "res");
        this.get_data();
        if (res.data == true) this.$message.success("修改用户成功！！");
        else this.$message.console.error("修改用户失败！！");
      });
    },
    // 监听pagesize 改变的事件
    handleSizeChange(newSize) {
      console.log(newSize);
    },

    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      console.log(newPage);
      this.page1 = newPage;
      this.get_data();
    },

    //根据id删除对应的用户信息
    async removeUserById(ids) {
      const confirmResult = await this.$confirm(
        "此操作将永久删除该用户, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      console.log(confirmResult);
      if (confirmResult !== "confirm") {
        return this.$message.info("已取消删除");
      }
      removeUser(ids).then((res) => {});
      this.$message.success("删除用户成功");
      this.get_data();
    },

    // 监听用户关闭对话框的事件
    addDialogClosed() {
      this.$refs.addFromRef.resetFields();
    },

    // 点击按钮 添加新用户
    addUser() {
      // 预验证
      this.$refs.addFromRef.validate((valid) => {
        if (!valid) return;
        // 从后端接参数
        addUsername(this.addFrom).then((res) => {
          console.log(res, "res");
          this.userList = res.data.data;
          // 重新渲染数据
          this.get_data();
          // 弹出添加用户对话框
          this.$message.success("添加用户成功");
          // 隐藏添加用户的对话框
          this.addDialogVisible = false;
        });
      });
    },
  },
};
</script>

<style scoped>
.el-table {
  margin-top: 15px;
}
.el-pagination {
  margin-top: 15px;
}
</style>